package com.baizhi.cmfz.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baizhi.cmfz.annotation.LogAnnotation;
import com.baizhi.cmfz.entity.Guru;
import com.baizhi.cmfz.service.GuruService;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.sound.midi.Soundbank;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/guru")
public class GuruController {
    @Autowired
    private GuruService guruService;
    @RequestMapping("/showAllGuru")
    @ResponseBody
    @LogAnnotation(value = "查询上师信息",type = "select")
    public Map showAllGuru(int page, int limit) {
        return guruService.showAllGuru(page, limit);
    }

    @RequestMapping("/insertGuru")
    @ResponseBody
    public Map insertGuru(Guru guru, MultipartFile file, HttpServletRequest request) throws IOException {
        String realPath = request.getServletContext().getRealPath("upload");
        //重命名
        String newFile = new Date().getTime() + "_" + file.getOriginalFilename();
        file.transferTo(new File(realPath + "/" + newFile));
        guru.setPic("/upload/" + newFile);
        return guruService.insertGuru(guru);
    }

    @RequestMapping("/deleteGuru")
    @ResponseBody
    public Map deleteGuru(int id) {
        return guruService.deleteGuru(id);
    }

    @RequestMapping("/showOne")
    @ResponseBody
    public Guru showOne(int id) {
        return guruService.showOne(id);
    }

    @RequestMapping("/updateGuru")
    @ResponseBody
    public Map updateGuru(Guru guru) {
        return guruService.updateGuru(guru);
    }

    @RequestMapping("/addAll")
    @ResponseBody
    public Map addAll(MultipartFile file, HttpServletRequest request) throws IOException {
        InputStream inputStream = file.getInputStream();
        Map map=new HashMap();
        HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
        HSSFSheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 0; i <= lastRowNum; i++) {
            HSSFRow row = sheet.getRow(i);
            Double gid = row.getCell(0).getNumericCellValue();
            String gname = row.getCell(1).getStringCellValue();
            String pic = row.getCell(2).getStringCellValue();
            String nickName = row.getCell(3).getStringCellValue();
            Double status = row.getCell(4).getNumericCellValue();
            Guru guru = new Guru();
            //guru.setGid(Integer.parseInt(gid));
            System.out.println(gid);
            guru.setGname(gname);
            guru.setPic(pic);
            guru.setNickName(nickName);
            //*guru.setStatus(Integer.parseInt(status));*//
            map=guruService.insertGuru(guru);
        }
        return map;
    }

  /*  @RequestMapping("/addAll")
    @ResponseBody
    public Map  addAll(MultipartFile file,HttpServletRequest request)throws Exception{
        InputStream inputStream=file.getInputStream();
        Map map=new HashMap();
        ExportParams exportParams = new ExportParams("上师数据", "guru");
        return  null;
    }*/
}
